import glob
import os.path
import os

srcdir = 'src'
builddir = 'build'

VariantDir(builddir, srcdir, duplicate = 0)

env = Environment(tools = ['mingw'], ENV = os.environ)

arch = 'WINDOWS'
if 'ARCH' in env['ENV']:
  arch = env['ENV']['ARCH']
else:
  arch = 'LINUX'

cppdirs = ['.', 'Model', 'View', 'Controller', 'Resources']
hdirs   = ['Headers/' + dir for dir in cppdirs]

SOURCES = []
for dir in cppdirs:
  SOURCES += glob.glob(os.path.join(srcdir, dir, '*.cpp'))
#for dir in hdirs:
#  SOURCES += glob.glob(os.path.join(srcdir, dir, '*.h'))

TARGETS = []
for src in SOURCES:
  TARGETS.append(src.replace(srcdir, builddir, 1))

# compiler flags
# -g = debug, -O3 = release
env.Append(CCFLAGS = ['-O3', '-Wall'])
if arch == 'WINDOWS':
  env.Append(CCFLAGS = ['-DWINDOWS'])

# linker flags
env.Append(LINKFLAGS = ['-static-libgcc', '-static-libstdc++'])

# libs
if arch == 'WINDOWS':
  env.Append(LIBS = ['mingw32'])
env.Append(LIBS = ['SDLmain', 'SDL', 'SDL_image', 'SDL_mixer'])

# compile
exe = env.Program(target = 'game', source = TARGETS)

# move to executable directory
targetname = "Executable/Tank"
if arch == 'WINDOWS':
  targetname = targetname + ".exe"
env.Command(targetname, exe, [ Move("$TARGET", "$SOURCE"),])